﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RecipeProject.Model;

namespace RecipeProject.ViewModel
{
    class IngredientVM : ViewModelBase
    {
        #region Fields
        private Ingredient _ingredient;
        private bool _isNewIngredient;
        private string _name;
        private string _qty;
        private string _uom;
        #endregion

        #region Constructor
        public IngredientVM(Ingredient ingredient)
        {
            _isNewIngredient = ingredient == null;
            _ingredient = ingredient;
        }
        #endregion

        #region Properties
        public bool IsNew { get { return _isNewIngredient; } }
        public String Name
        {
            get { return IsNew ? _name : _ingredient.Name; }
            set
            {
                if (!IsNew) throw new NotImplementedException();
                _name = value;
            }
        }
        public String Qty
        {
            get { return IsNew ? _qty : _ingredient.Quantity.ToString(); }
            set
            {
                if (!IsNew) throw new NotImplementedException();
                _qty = value;
            }
        }
        public String UOM
        {
            get { return IsNew ? _uom : _ingredient.Measurement; }
            set
            {
                if (!IsNew) throw new NotImplementedException();
                _uom = value;
            }
        }
        public Ingredient GetIngredient 
        { 
            get 
            {
                return IsNew ?
                    new Ingredient
                    {
                        Name = _name,
                        Quantity = Mathematics.RationalNumbers.Rational.Parse(_qty),
                        Measurement = _uom
                    }
                    : _ingredient;
            }
        }
        #endregion


    }
}
